<template>
    <div>
      <input type="number" v-model="user"  placeholder="请输入账号">
      <input type="password" v-model="password" placeholder="请输入密码">
      <div v-if="imgurl!=''">
        <a-image :src="imgurl"></a-image>
        <input type="text" v-model="isc" placeholder="验证码">
      </div>
      
      <a-button type="primary" :loading="loading" @click="getmsg">登入</a-button>
      
    </div>
    
    <div class="app-container">
      <h3>日期选择</h3>
      <a-space direction="vertical" :size="12">
        <a-range-picker v-model:value="dayl" :disabled-date="disabledDate" />
        
        <!-- 日志选择 -->
          <a-select
          ref="select"
          v-model:value="templog"
          style="width: 120px"
        >
          <a-select-option value="d">日志</a-select-option>
          <a-select-option value="w">周志</a-select-option>
          <a-select-option value="m" >月志</a-select-option>
        </a-select>
        <div>大模型输出专业的文章<input type="text" v-model="speciality" placeholder="不填默认写年级专业日志"></div>
        <a-progress v-show="loadsend" :percent="percent" status="active" />
        <button  @click="sendtwo">发送</button>
        <button  @click="tqiandao">签到</button>
     </a-space>
     
    </div>
    <h5>下面可以默认不管。大模型次数用完再填写</h5>
    <div><input type="text" placeholder="APPID" v-model="key.APPID"><input type="text" v-model="key.API_SECRET" placeholder="API_SECRET"><input v-model="key.API_KEY" type="text" placeholder="API_KEY"><button @click="mmkey">确认大模型密钥</button></div>
    <div style="margin-top: 100px;">
      <a-table :dataSource="dataSource" :columns="columns" >
        <template #bodyCell="{record,column}">
          <template v-if="column.dataIndex=='startDate'">{{record.startDate +'-'+ record.endDate}}</template>
          
        </template>
      </a-table>
    </div>
</template>
<!-- <script type="text/javascript" setup src="https://webapi.amap.com/maps?v=1.4.15&key=d8fae8000f3996bf2f0c32337d69f768">
        window._AMapSecurityConfig = {
            securityJsCode:'5496f84bfdc5bd9934c359ebcbe070c0'
        }
</script> -->


<script setup>
import { onMounted, onUpdated, ref, watch } from "vue";
import { bigmodel,getpersoninfo,writemsg,qiandao } from "../../api/index";
import { changekey } from "../../api/pramirymodel";
import { message } from "ant-design-vue";
const key=ref({
  APPID:'Appid',
  API_SECRET:'Api-secret',
  API_KEY:'apikey',
  
})
const percent=ref(0);
const speciality=ref('')
const loading=ref(false);
const loadsend=ref(false);
let rangetime=[];
const templog=ref('d');
const dayl=ref('');
const isc=ref('');
const imgurl=ref('');
const personmsg=ref({});
const captcha=ref('')
const user=ref("13576052637");
const password=ref("lzm1725398914");
const dataSource=ref([{}]);
const columns=ref([
          {
            title: '姓名',
            dataIndex: 'loginer',
            key: 'name',
          },
          {
            title: '完成日志篇数',
            dataIndex: 'blogsCount',
          },
          {
            title: '完成周志篇数',
            dataIndex: 'wblogsCount',
          },
          {
            title: '完成月志篇数',
            dataIndex: 'mblogsCount',
          },
          {
            title: '截止时间',
            dataIndex: 'startDate',
            key: 'phone',
          },
          {
            title: '年级',
            dataIndex: 'plan',
            key: 'age',
          },
          {
            title: '学校',
            dataIndex: 'schoolName',
            key: 'address',
          }
        ])
// 大模型信息用户信息
let userinfo={
  user:"lzm",
  date:"2023.12.14",
  endDate:"2023.12.19",
  speciality:"",
  log:"d"//w,m,

};
const mmkey=()=>{
  console.log(key.value,"jj");
  changekey(key.value)
}
const tqiandao=()=>{
  qiandao()
}
const sendtwo=()=>{
  if (rangetime==false) {
    message.warn("请选择日期")
    return
  }
  let gi=parseInt({d:0,w:6,m:29}[userinfo.log])
  let numday=1;
  let op=false;
  let gangetime=rangetime;
  let dd='';
  loadsend.value=true
  if (userinfo.log=='w') {
    dd='w'
    numday=7
    gangetime=rangetime.filter((item,index,arry)=>{
      const ttime=(item.getTime()-new Date(dataSource.value[0].startDate.replaceAll('.',"/")).getTime())/(1000*24*60*60)
      
      if((ttime%numday)==0){
        op=true
        if ((arry.length-index)<numday) {
          op=false
        }
      }
      return  ttime== 0 || (ttime%numday) == 0 || op ;
    })
    if(gangetime.length%7==1){
      gangetime.pop()
    } 
  }
  if(userinfo.log=='m'){
    dd='m'
    numday=30
    gangetime=rangetime.filter((item,index,arry)=>{
      const ttime=(item.getTime()-new Date(dataSource.value[0].startDate.replaceAll('.',"/")).getTime())/(1000*24*60*60)
      
      if((ttime%numday)==0){
        op=true
        if ((arry.length-index)<numday) {
          op=false
        }
      }
      return  ttime== 0 || (ttime%numday) == 0 || op ;
    })
    if(gangetime.length%30==1){
      gangetime.pop()
    } 
  }
  
  async function zhixin(){
    let gg=1;
    for(let i=0;i<gangetime.length;i+=gi){
      
        
        loadsend.value=true
        userinfo.date=gangetime[i].getFullYear()+'.'+(gangetime[i].getMonth()+1)+'.'+gangetime[i].getDate();
        userinfo.endDate=gangetime[i+gi].getFullYear()+'.'+(gangetime[i+gi].getMonth()+1)+'.'+gangetime[i+gi].getDate()
        await bigmodel(userinfo, personmsg.value).then(val=>{
                message.success(val+' '+gg);
                getpersoninfo(userinfo.log).then(val=>{
                  if (!val.data.data==[]) {
                    dataSource.value[0][userinfo.log+"blogsCount"]=val?.data?.data[0]?.blogsCount || "无法查询"
                  }
                
              })
              gg++
              percent.value=Math.round(((i+1)/gangetime.length)*100)
          }).finally(()=>{
              i++
          })
        
    } 
    
    setTimeout(()=>{
    percent.value=0,
    loadsend.value=false
    },5000)
  }
  zhixin()
  
  

  
  
    
   

  
}
const disabledDate= current=>{
 
  return current && current < new Date(dataSource.value[0].startDate) || current >= new Date();
}
// 刷新使用
//flashperson()

function getmsg() {
  loading.value=true;
  writemsg(user.value,password.value,isc.value).then(val=>{
    if(typeof val=='string'){
      imgurl.value=val;
      isc.value=''
    }else{
      personmsg.value=val;
      dataSource.value=[personmsg.value];  
      speciality.value=dataSource.value[0]?.plan.slice(3,7) || '';
      console.log(personmsg.value);
      imgurl.value=''
    }
    loading.value=false;
  })
}
function getDaysBetween(startDate, endDate) {
      const start = new Date(startDate);
      const end = new Date(endDate);
      console.log(start,"hhhhh",end);
      const days = [];

      while (start <= end) {
        days.push(new Date(start));
        start.setDate(start.getDate() + 1);
      }
      
      return days;
    }

onMounted(()=>{
  // 获取个人信息
  
  getmsg()
  setTimeout(() => {
  
  getpersoninfo('w').then(val=>{
                  
                  dataSource.value[0].wblogsCount=val.data.data[0].blogsCount
                })
  getpersoninfo('m').then(val=>{
                  if (Object.keys(dataSource.value[0]).indexOf('blogsCount')==-1) {
                    dataSource.value=[{...dataSource.value[0],...val.data.data[0]}]
                  }
                  dataSource.value[0].mblogsCount=val.data.data[0].blogsCount
                  
                })
  }, 5000);
})


// 范围时间
watch(dayl,(day,old)=>{
  console.log(day,typeof day);
  if (day) {
    const time= day[0].$d;
    const endtime=day[1].$d;
    userinfo.date=time.getFullYear()+'.'+(time.getMonth()+1)+'.'+time.getDate();
    userinfo.endDate=endtime.getFullYear()+'.'+(endtime.getMonth()+1)+'.'+endtime.getDate();
    console.log(userinfo.date,userinfo.endDate);
    const startDate=userinfo.date.replaceAll('.',"-");
    const endDate=userinfo.endDate.replaceAll('.',"-");
    rangetime = getDaysBetween(startDate, endDate);
    console.log(rangetime,'time');
    console.log(endDate,"end");
   
    
  }else{
    console.log('时间清除');
  }

})
// 日月志选择
watch(templog,(log,oldlog)=>{
  userinfo.log=log;
})
// 实习选择
watch(speciality,(newval,oldv)=>{
  userinfo.speciality=newval
})

</script>

<style>

</style>